﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ARchGL.Platform.Utils
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    /// <summary>
    /// 加密
    /// </summary>
    public class DES
    {
        //默认密钥向量
        private static readonly byte[] Keys = { 0x11, 0x22, 0x33, 0x44, 0xAA, 0xBB, 0xCC, 0xDD };

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
        public static string Decode(string decryptString, string decryptKey)
        {
            return Decode(decryptString, decryptKey, Keys);
        }

        public static string Decode(string decryptString, string decryptKey, byte[] decryptIV)
        {
            try
            {
                var rgbKey = new byte[32];
                rgbKey = System.Text.UTF8Encoding.UTF8.GetBytes(decryptKey);
                var ivArray = new byte[32];
                var rgbIV = decryptIV;
                var inputByteArray = Convert.FromBase64String(decryptString);
                var DCSP = new DESCryptoServiceProvider
                {
                    Padding = PaddingMode.ANSIX923
                };
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
        public static string Encode(string encryptString, string encryptKey)
        {
            return Encode(encryptString, encryptKey, Keys);
        }

        public static string Encode(string encryptString, string encryptKey, byte[] encryptIV)
        {
            var keyArray = new byte[32];
            keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(encryptKey);
            var ivArray = new byte[32];
            ivArray = encryptIV;
            var toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(encryptString);
            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.IV = ivArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.CBC;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
            var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
    }
}
